package de.hawhamburg.shc.livingplace;


import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONHelper {

        private static int id = 0;
        private static final String ID_TEXT = "christian_philip";
        private static final String SERVER = "172.16.0.200";
        private static final String PORT = "12349";
        

        static String SLEEPING_ROOM = "sleeping";
        
        static String WINDOW_TOPIC = "WINDOW.CONTROL";
        static String LIGHT_TOPIC = "LP.LIGHTCONTROL";
        static String LIGHT_ON = "_light_on";
        static String LIGHT_OFF = "_light_off";
        static String CURTAIN_OPEN = "_curtain_open";
        static String CURTAIN_CLOSE = "_curtain_close";
        static String BLINDS = "blinds_";
        static String OPEN = "_open";
        static String CLOSE = "_close";

        public static String[] createMessage(String topic, String action,
                        Map<String, String> valueMap) {
                Map<String, Object> changeMap = new HashMap<String, Object>();
                changeMap.put("values", new JSONObject(valueMap));
                changeMap.put("Id", ID_TEXT + id++);
                changeMap.put("Version", null);
                changeMap.put("action", action);
                JSONObject msg = new JSONObject(changeMap);

                String[] params = new String[5];
                params[0] = SERVER;
                params[1] = PORT;
                params[2] = topic;
                params[3] = "topic";
                params[4] = msg.toString();
                Log.d(JSONHelper.class.getSimpleName(), msg.toString());

                return params;
        } 
        
        public static String[] createWindowMessage(String topic, String message) {
                JSONObject msg;
                try {
                        msg = new JSONObject(message);

                        String[] params = new String[5];
                        params[0] = SERVER;
                        params[1] = PORT;
                        params[2] = topic;
                        params[3] = "topic";
                        params[4] = msg.toString();
                        Log.d(JSONHelper.class.getSimpleName(), msg.toString());

                        return params;

                } catch (JSONException e) {
                        e.printStackTrace();
                }

                return null;
        }
     

        public static String[] createHeatingMessage(String topic, String module, String hexValue) {
                Map<String, Object> changeMap = new HashMap<String, Object>();
                changeMap.put(module, hexValue);
                changeMap.put("Id", ID_TEXT + id++);
                changeMap.put("Version", null);
                changeMap.put("action", "");
                JSONObject msg = new JSONObject(changeMap);

                String[] params = new String[5];
                params[0] = SERVER;
                params[1] = PORT;
                params[2] = topic;
                params[3] = "topic";
                params[4] = msg.toString();
                Log.d(JSONHelper.class.getSimpleName(), msg.toString());

                return params;
        }
        
        public static String[] createLightMessage(String room, boolean isChecked){
        	
        	Map<String,String> valueMap = new HashMap<String, String>();
	        valueMap.put("fadeTime", "0");
	        if(isChecked)return JSONHelper.createMessage(LIGHT_TOPIC, room+LIGHT_ON , valueMap);
	        return JSONHelper.createMessage(LIGHT_TOPIC, room+LIGHT_OFF , valueMap);
        	
        }

        public static String[] createLightMessage(String room, boolean isChecked, String fadeTime){
        	Map<String,String> valueMap = new HashMap<String, String>();
	        valueMap.put("fadeTime", fadeTime);
        	if(isChecked)return JSONHelper.createMessage(LIGHT_TOPIC, room+LIGHT_ON , valueMap);
        	return JSONHelper.createMessage(LIGHT_TOPIC, room+LIGHT_OFF , valueMap);
        }
  
        public static String[] createCurtainMessage(String room, boolean isChecked){
    	    Map<String,String> valueMap = new HashMap<String, String>();
    	    if(isChecked) return JSONHelper.createMessage(LIGHT_TOPIC, room+CURTAIN_OPEN , valueMap); 
    	    return JSONHelper.createMessage(LIGHT_TOPIC, room+CURTAIN_CLOSE , valueMap);
        }
           
       
       public static String[] createBlindMessage(String room, boolean isChecked){
    	  Map<String,String> valueMap = new HashMap<String, String>();
    	  if(isChecked) return JSONHelper.createMessage(LIGHT_TOPIC, BLINDS+room+OPEN , valueMap);  
    	  if(room.equals(SLEEPING_ROOM)){
    		  //to close the blinds in the sleeping room
    		  return JSONHelper.createMessage(LIGHT_TOPIC, "blinds_close" , valueMap);
    	  }
    	  return JSONHelper.createMessage(LIGHT_TOPIC, BLINDS+room+CLOSE , valueMap);
       }
       
       public static String[] createWindowMessageString(String room, int value){	
    	   
			//create a json-array-hashmap to send the information correctly
			HashMap<String, JSONArray> arguments = new HashMap<String, JSONArray>(); 
			JSONArray innerArguments = new JSONArray();
		    innerArguments.put(""+value);
			innerArguments.put("FAST");
			arguments.put(room, innerArguments);
			return JSONHelper.createWindowMessage(WINDOW_TOPIC,arguments.toString());
       }
}